<?php
/**
 * XuYuan Project
 *
 * @package	XueYuan 
 * @author	Jiuchi Team
 * @since	Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * User controller class
 * @author Wayne Yu
 */
class Comments extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->library('apioutput');
		$this->load->library('msgblock');
		$this->load->helper('url');
		$uid = $this->session->userdata('uid');
		if(empty($uid)){
			redirect(site_url('login'));
		}
		$this->load->model('user_model');
		$this->load->model('user_dynamic_model');
		$this->load->model('circle_model');
		$this->load->model('circle_category_model');
		$this->load->model('circle_user_model');
		$this->load->model('circle_admin_model');
		$this->load->model('topic_model');
		$this->load->model('comments_model');
	}
	
	/**
	 * Create page
	 */
	public function create(){
		$circleCategory = $this->circle_category_model->retrieve();
		$data = array(
				'cirCate'	=>	$circleCategory
		);
		$this->load->view('circle/createCircle', $data);
	}
	
	/**
	 * create circle post
	 */
	public function createPost(){
		$input = $this->input->post();
		if(empty($input['name'])|| empty($input['category']) || empty($input['description'])){
			die("<script>alert('该圈名已被创建！');window.location.href='".site_url('circle/create')."';</script>");
		}
		$data = array(
				'circle_category_id'	=>	$input['category'],
				'name'					=>	$input['name'],
				'description'			=>	$input['description'],
				'user_number'			=>	1,
				'creator'				=>	$this->session->userdata('uid')
		);

		if($this->circle_model->checkCircleName($data['name']) == FALSE){
			die("<script>alert('该圈名已被创建！');window.location.href='".site_url('circle/create')."';</script>");
		}
		if($this->circle_admin_model->getCircleCount($data['creator']) >= 3){
			die("<script>alert('您管理的圈子数量过多');window.location.href='".site_url('circle/create')."';</script>");
		}
		//save
		$this->circle_model->begin();
		$cid = $this->circle_model->createEntity($data);
		if($cid <= 0){
			die("<script>alert('创建失败，请稍后重试！');window.location.href='".site_url('circle/create')."';</script>");
		}
		$data = array(
				'circle_id' => $cid,
				'user_id'	=> $this->session->userdata('uid'),
				'is_primary'=> '1'
		);
		$res = $this->circle_admin_model->createEntity($data);
		if(!$res){
			$this->circle_model->rollback();
			die("<script>alert('创建失败，请稍后重试！');window.location.href='".site_url('circle/create')."';</script>");
		}
		$data = array(
				''
		);
		$res = $this->circle_user_model->createEntity($data);
		if(!$res){
			$this->circle_model->rollback();
			die("<script>alert('创建失败，请稍后重试！');window.location.href='".site_url('circle/create')."';</script>");
		}
		
		$this->circle_model->commit();
		
		die("<script>alert('圈子创建成功');window.parent.$.fancybox.close();</script>");
	}
	
	public function view($topic_id){
		$topicInfo = $this->topic_model->getInfo($topic_id);
		$userInfo = $this->user_model->getInfo($topicInfo['user_id']);
		$data = array(
				'topicInfo'	=>	$topicInfo,
				'userInfo'	=>	$userInfo
		);
		$this->load->view('topic/view', $data);
	}
	
	/**
	 * get topics' comments
	 */
	public function getTopicComments($tid){
		$input = $this->input->post();
		$pageSize = isset($input['amount'])?$input['amount']:5;
		$laseItem = isset($input['last'])?$input['last']:0;
		
		$data = $this->comments_model->getTopicComments($tid, $pageSize, $laseItem);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['user_id']);
			$data[$key] = array(
					'headportrait'	=>	'<a href="'.site_url('user/view/'.$val['user_id']).'"><img style="width:35px;" src="'.(isset($uInfo['headportrait'])?$uInfo['headportrait']:'').'" class="img-avatar"></a>',
					'name'			=>	isset($uInfo['nickname'])?'<a href="'.site_url('user/view/'.$val['user_id']).'">'.$uInfo['nickname'].'</a>':'',
					'comment_photo'	=>	($val['comments_img'])?'<img src="'.$val['comments_img'].'" class="rep-img">':'',
					'content'		=>	$val['content'],
					'viewbutton'	=>	'<a href="'.site_url('comments/view/'.$val['id']).'">查看回复</a>'
			);
		}
		echo json_encode($data);
	}
}